#include "Rfid_api.h"
#include "iso14443b.h"

#if _RFID_FUNCTION_ENABLE_

bool TypeBHalt(uint8_t *pSnr)
{
  uint16_t DataSize = 0;
  uint8_t HaltResult = 0, OperationDataBuff[MAXRLEN] = {0};

  if (pSnr != NULL)
  {
    OperationDataBuff[DataSize++] = 0x50;
    memcpy(&OperationDataBuff[DataSize], pSnr, 4);
    DataSize += 4;

    RfidPcdSetTimer(5);
    HaltResult = RfidPcdCommunication(PCD_TRANSCEIVE, OperationDataBuff, 5, OperationDataBuff, &DataSize);
    if (HaltResult == MI_OK)
      return true;
  }

  return false;
}

bool TypeBWakeup(uint8_t *pSnr)
{
  uint16_t DataSize = 0;
  uint8_t WakeupResult = 0, OperationDataBuff[MAXRLEN] = {0};

  if (pSnr != NULL)
  {
    OperationDataBuff[DataSize++] = 0x05;
    OperationDataBuff[DataSize++] = 0x00;
    OperationDataBuff[DataSize++] = 0x08;

    RfidPcdSetTimer(5);
    WakeupResult = RfidPcdCommunication(PCD_TRANSCEIVE, OperationDataBuff, DataSize, OperationDataBuff, &DataSize);
    if (WakeupResult == MI_OK)
    {
      memcpy(pSnr, OperationDataBuff + 1, 4);
      return true;
    }
  }

  return false;
}

bool TypeBRequest(uint8_t *pSnr)
{
  uint16_t DataSize = 0;
  uint8_t RequestResult = 0, OperationDataBuff[MAXRLEN] = {0};

  if (pSnr != NULL)
  {
    OperationDataBuff[DataSize++] = 0x05;
    OperationDataBuff[DataSize++] = 0x00;
    OperationDataBuff[DataSize++] = 0x00;

    RfidPcdSetTimer(5);
    RequestResult = RfidPcdCommunication(PCD_TRANSCEIVE, OperationDataBuff, DataSize, OperationDataBuff, &DataSize);
    if (RequestResult == MI_OK)
    {
      memcpy(pSnr, OperationDataBuff + 1, 4);
      return true;
    }
  }

  return false;
}

bool TypeBSelect(uint8_t *pSnr)
{
  uint16_t DataSize = 0;
  uint8_t SelectResult = 0, OperationDataBuff[MAXRLEN] = {0};

  if (pSnr != NULL)
  {
    OperationDataBuff[DataSize++] = 0x1D;
    memcpy(&OperationDataBuff[DataSize], pSnr, 4);
    DataSize += 4;
    OperationDataBuff[DataSize++] = 0x00;
    OperationDataBuff[DataSize++] = 0x08;
    OperationDataBuff[DataSize++] = 0x01;
    OperationDataBuff[DataSize++] = 0x01;

    RfidPcdSetTimer(5);
    SelectResult = RfidPcdCommunication(PCD_TRANSCEIVE, OperationDataBuff, DataSize, OperationDataBuff, &DataSize);
    if (SelectResult == MI_OK)
      return true;
  }

  return false;
}

bool TypeBGetUID(uint8_t *pSnr, uint8_t *pSnrLength)
{
  uint16_t DataSize = 0;
  uint8_t GetUIDResult = 0, OperationDataBuff[MAXRLEN] = {0};

  if ((pSnr != NULL) && (pSnrLength != NULL))
  {
    OperationDataBuff[DataSize++] = 0x00;
    OperationDataBuff[DataSize++] = 0x36;
    OperationDataBuff[DataSize++] = 0x00;
    OperationDataBuff[DataSize++] = 0x00;
    OperationDataBuff[DataSize++] = 0x08;

    RfidPcdSetTimer(5);
    GetUIDResult = RfidPcdCommunication(PCD_TRANSCEIVE, OperationDataBuff, DataSize, OperationDataBuff, &DataSize);
    if (GetUIDResult == MI_OK)
    {
      *pSnrLength = (DataSize / 8) - 2;
      memcpy(pSnr, OperationDataBuff, (DataSize / 8) - 2);
      return true;
    }
  }

  return false;
}

#endif
